
function freshOrderCart(datalists){
    if(datalists==null){$("#item-sec").html("当前购物车空荡荡...");return;}
    $("#item-sec").html("");
    let sum=0;
    for(let data of datalists){
        let price = formatPrice(data.product.price);
        $div = $("<div class=\"mod_cell ui_plr0 ui_mt15 qb_mb15 item\" id=\"0\" last-count=\"1\" price=\"500\">\n" +
            "<div class=\"ui_plr10\">\n" +
            "<ul class=\"mod_list mod_list_hr qb_fs_s\">\n" +
            "<li class=\"list_item qb_mb10 qb_bfc\">"
            +"<a href="+"'/productdetail?productId="+data.product.productId+"' class=\"bfc_f\">\n" +
            "                <img src='"+"http://image.pq.com/"+data.product.imgUrl+"'></a>"+
            "<div class=\"bfc_c\">\n" +
            "<p>"+data.product.title+"</p>\n" +
            "<p>单价：<span class=\"ui_color_strong\">￥"+price+
            "元</span></p>\n" +
            "</div>\n" +
            "</li>\n" +
            " </ul>\n" +
            "</div>\n" +
            "<div class=\"qb_tar ui_p10 qb_fs_s ui_bg_color_weak ui-bt\">\n" +
            "<label><input value="+data.orderCartId+" type = 'checkbox'  value = \"\"  />选中 </label>"+
            "<button onclick='deleteCart("+data.orderCartId+")'>删除</button>"+
            "<i class=\"qb_icon icon_trash ui_mr30 icon_delete\"></i>数量："+
            "<button  onclick='changeProductCounts(0,"+data.orderCartId+")'>-\n" +
            "    </button>"+
            "<input id='input"+data.orderCartId+"' type=\"tel\" class=\"ui_textinput ui_textinput_tiny count\" value='"+data.productCounts+"'>"+
            "<button  onclick='changeProductCounts(1,"+data.orderCartId+")'>\+</button>" +
            "<span class=\"qb_tof\" style=\"width: 100px\">共计：<span class=\"ui_color_strong single-total\">￥"+data.productCounts*price+"元</span></span></div>\n" +
            "            </div>");
        $("#item-sec").append($div);
        sum += data.productCounts*price;
    }
    $div = $("<div class=\"ui_pb10 ui_clearfix qb_fs_s\">" +
        "                            <div class='qb_fl'>" +
        "<button onclick='checkAll()' class='choose-all'><input  type = 'checkbox'  value = \"\"  />全选 </button>"+
        "                                <span class=\"ui_mr30 ui_pt10\"><i class=\"qb_icon icon_checkbox icon_checkbox_checked\" id=\"choose-all\"></i></span>\n" +
        "                                总价：<span class=\"ui_color_strong\">￥"+sum+"元<span id=\"total\"></span></span>\n" +
        "                            </div>\n" +
        "                            <span onclick='orderSubmit()' class=\"mod_btn btn_strong qb_fr confirm_order\">去结算</span>\n" +
        "                        </div>");
    $("#item-sec").append($div);
}
// function dealTotalPrice(datalists){
//     let length = $("span .single-total").length;
//     for (let i = 0; i <length ; i++) {
//         $div = $("span .single-total :eq("+i+")");
//         let counts = $div.parent().parent().children("input").val();
//         let price = datalists[i].product.price;
//         $div.html("￥"+formatPrice(counts*price)+"元");
//     }
// }
function changeProductCounts(flag,cartId) {
    if(flag==1||flag==0) {
        let counts = parseInt($("#input" + cartId).val());
        let url = "/doUpdateOrderCartCountsByCartId";
        let param={"cartId":cartId};
        if (flag == 0) {
            if (counts == 1) {
                alert("最少购买一个");
                return;
            }
            counts--;
        } else if (flag == 1) {
            counts++;
        }
        param["productCounts"] =counts;
        $.post(url,param,function (result) {
            if(result.status==200){
                doFindOrderCartByUserId();
            }
        })

    }
}
function deleteCart(cartId){
    if(confirm("是否删除?")) {
        let url = "/doUpdateOrderCartFlagByCartId"
        let param = {"cartId": cartId};
        $.post(url, param, function (result) {
            if (result.status == 200) {
                doFindOrderCartByUserId();
                alert("删除成功!!!")

            }else{alert("删除失败!!!")}
        })
    }

}
function doFindOrderCartByUserId(){
    let url="/doFindOrderCartByUserId";
    let params = {"flag":0};
    $.post(url,params,function(result) {
        let datalists = result.data;
        freshOrderCart(datalists);
    })
}

function checkAll(){
    let checkflag=true;
    $(".choose-all :checkbox[checked]").each(function(i){
        checkflag=false;
    });
    if(!checkflag){
    $(".qb_tar :checkbox").prop("checked",true);
    $(".choose-all :checkbox").prop("checked",true);
    return;
    }
    $(".qb_tar :checkbox").prop("checked",false);
    $(".choose-all :checkbox").prop("checked",false);
}
function orderSubmit(){
    let url = "/doSaveOrder";
    let orderCartIds = getOrderCartIds();
    let params={"orderCartIds":orderCartIds};
    // alert(params);
    $.post(url,params,function(result){
        if(result.status==200){
            let orderId = result.data;
            window.location.href= "/orderSubmit?orderId="+orderId;
        }else{
            alert("提交失败");
        }
    })
}
function getOrderCartIds(){
    let orderCartIds=[];
    $(".qb_tar :checkbox[checked]").each(function(i){
        orderCartIds.push(parseInt($(this).val()));
    });
    orderCartIds = orderCartIds.join(",");
    return orderCartIds;
}